extends INodeFactory


class_name NodeDuplicateFactory
"""
负责从NodeTree中复制指定节点对象
"""


## 引用的节点
@export var template_node: Node = null

@export_category("Flag")
## 拷贝时的参数
@export_flags("SIGNALS:1", "GROUPS:2", "SCRIPTS:4", "USE_INSTANTIATION:8") var duplicate_flag: int = 15
@export_category("")


func __check_exports() -> Dictionary:
    return IComponent.merge({
        "template_node": __find_template_node.bind(self, template_node),
    }, super.__check_exports())

func __find_template_node(node: Node, optional_node: Node) -> Node:
    if optional_node:
        return optional_node
    # 先查找是否有冻结的节点,再在冻结的节点下寻找模板节点
    var frozen_node: Node = IComponent.find_down(node, func(x): return IComponent.is_component(x) and x.is_frozen)
    if not frozen_node:
        frozen_node = node
    var tmpl_node: Node = IComponent.find_down(frozen_node, func(x): return IComponent.is_component(x) and not x.is_frozen)
    if tmpl_node:
        return tmpl_node
    return IComponent.find_down(frozen_node, func(x): return x is Node)

# 生成一个对象
func __make_one() -> Node:
    return template_node.duplicate(duplicate_flag)
